package com.batyuta;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.util.ArrayList;
import java.util.List;

/**
 * Created by IntelliJ IDEA.
 * User: alexey
 * Date: 6/20/11
 * Time: 5:57 PM
 * To change this template use File | Settings | File Templates.
 */
public class Graph extends JFrame implements MouseListener, ActionListener {
    public static final int mvX = 140;
    public static final int mvY = 140;
    public static final int INT = TriangleProvider.getTriangles().size();
    public static final int COLUMNS_COUNT = 3;
    public static final float BIG_LINE = 2.0f;

    public static void main(String args[]) {
        Graph that = new Graph();
        that.setVisible(true);
    }

    public Graph() {
        addMouseListener(this);
        setLayout(new BorderLayout());
        setSize(mvX * COLUMNS_COUNT * 2, mvY * INT / COLUMNS_COUNT + mvY/2);
        //setMinimumSize(new Dimension(mvX * (COLUMNS_COUNT*2)  , mvY * (INT/COLUMNS_COUNT)+mvY/2));
        //setMaximumSize(new Dimension(mvX * (COLUMNS_COUNT*2)  , mvY * (INT/COLUMNS_COUNT)+mvY/2));
        //setMinimumSize(new Dimension(mvX , mvY * INT));
        setTitle("My Drawing tool");
        //paint1(getGraphics());
    }

    private List<Point> points = new ArrayList<Point>();

    public void paint(Graphics g) {
        g.setColor(Color.white);
        g.fillRect(0, 0, getSize().width, getSize().height);
        int i = 0;
        for (ITriangle triangle : TriangleProvider.getTriangles()) {
            showTr(g, triangle, mvX * (i % COLUMNS_COUNT * 2), mvY * (i++ / COLUMNS_COUNT));
        }
    }

    private void showTr(Graphics g1, ITriangle triangle, int left, int up) {
        Graphics2D g = (Graphics2D) g1;
        int a = triangle.getLen1();
        int b = triangle.getLen2();
        int c = triangle.getLen3();


        Point a1 = new Point(mvX + left, mvY + up);
        Point a2 = new Point(mvX + left + a, mvY + up);

        double v = (Math.pow(a, 2) -
                Math.pow(b, 2) +
                Math.pow(c, 2)) / (2 * a);
        double dd = (Math.sqrt(c * c - v * v));

        if (dd < a) {
            g.setColor(Color.cyan);
            //g.drawLine((int) (mvX + left + v), (int) (mvY + up - dd), (int) (mvX + left + v), (int) (mvY + up + dd));
            g.drawLine((int) (mvX + left + v), (int) (mvY + up - dd), (int) (mvX + left + v), (int) (mvY + up));

            g.setStroke(new BasicStroke(BIG_LINE));
            g.setColor(Color.green);
            //g.drawLine(a1.x, a1.y, (int) (mvX + left + v), (int) (mvY + up + dd));
            g.drawLine(a1.x, a1.y, (int) (mvX + left + v), (int) (mvY + up - dd));

            g.setColor(Color.red);
//            g.drawLine(a2.x, a2.y, (int) (mvX + left + v), (int) (mvY + up + dd));
            g.drawLine(a2.x, a2.y, (int) (mvX + left + v), (int) (mvY + up - dd));
            g.setStroke(new BasicStroke(1.0f));
        }
        Point h = new Point((int) (mvX + left + v), mvY + up);
        g.setColor(Color.blue);
        rr(g, h, 1);

        g.setColor(Color.black);
        g.setStroke(new BasicStroke(BIG_LINE));
        g.drawLine(a1.x, a1.y, a2.x, a2.y);
        g.setStroke(new BasicStroke(1.0f));
        g.setColor(Color.LIGHT_GRAY);
        g.drawLine(mvX + left-mvX/2, mvY + up, mvX*2 + left, mvY + up );
        g.setColor(Color.cyan);
        g.drawString("Is it triangle = " + triangle.isValid().toString(), 10 + left, up + mvY / 2);
        g.setColor(Color.black);
        g.drawString("a = " + a, 10 + left, up + mvY / 2 + 15);
        g.setColor(Color.red);
        g.drawString("b = " + b, 10 + left, up + mvY / 2 + 30);
        g.setColor(Color.green);
        g.drawString("c = " + c, 10 + left, up + mvY / 2 + 45);

        g.setColor(Color.green);
        rr(g, a1, c);
        g.setColor(Color.red);
        rr(g, a2, b);
    }

    private void rr(Graphics g, Point a, int r1) {
        g.drawOval(a.x - r1, a.y - r1, r1 * 2, r1 * 2);
    }

    public void mousePressed(MouseEvent e) {
    }

    public void mouseReleased(MouseEvent e) {
    }

    public void mouseClicked(MouseEvent e) {
    }

    public void mouseEntered(MouseEvent e) {
    }

    public void mouseExited(MouseEvent e) {
    }

    public void actionPerformed(ActionEvent e) {
    }
}
